package com.youi.shop.controller;


import com.youi.shop.commons.result.RespEntity;
import com.youi.shop.commons.result.StatusCode;
import com.youi.shop.pojo.qo.member.MemberLoginParams;
import com.youi.shop.pojo.qo.member.MemberRegisterParams;
import com.youi.shop.pojo.vo.member.MemberVO;
import com.youi.shop.service.MemberService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author xxf
 * @since 2020-11-09
 */
@RestController
@RequestMapping("/member")
@Api(tags = "会员业务接口")
public class MemberController {

    @Resource
    MemberService memberService;

    /**
     * 会员登录
     *
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("会员登录")
    public RespEntity<MemberVO> login(@Validated @RequestBody MemberLoginParams params) {
        //用户名密码正则校验
        return memberService.selectOne(params);
    }

    /**
     * 会员注册
     *
     * @param params
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("会员注册")
    public RespEntity<MemberVO> register(@Validated @RequestBody MemberRegisterParams params) {
        //非空校验
        //调用业务层判断用户名是否存在
        RespEntity respEntity = null;
        boolean flag = memberService.insert(params);
        if (flag) {
            //注册成功返回消息
            MemberVO memberVO = new MemberVO();
            BeanUtils.copyProperties(params, memberVO);
            respEntity = RespEntity.success(RespEntity.success(memberVO));
        } else {
            //返回失败
            respEntity = RespEntity.error(StatusCode.REGISTER_ERROR);
        }
        return respEntity;
    }


}
